/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chat;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NetTools {

    public static boolean isPortUsed(int port) {
        try {
            ServerSocket ss = new ServerSocket(port, 10, InetAddress.getByName(getIP()));
            ss.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String getLocalHostIP() {
        String ip;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress();
        } catch (Exception ex) {
            ip = "";
        }
        return ip;
    }

    public static String getLocalHostName() {
        String hostName;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            hostName = addr.getHostName();
        } catch (Exception ex) {
            hostName = "";
        }
        return hostName;
    }

    public static String[] getAllLocalHostIP() {
        String[] ret = null;
        try {
            String hostName = getLocalHostName();
            if (hostName.length() > 0) {
                InetAddress[] addrs = InetAddress.getAllByName(hostName);
                if (addrs.length > 0) {
                    ret = new String[addrs.length];
                    for (int i = 0; i < addrs.length; i++) {
                        ret[i] = addrs[i].getHostAddress();
                    }
                }
            }

        } catch (Exception ex) {
            ret = null;
        }
        return ret;
    }

    public static String[] getAllHostIPByName(String hostName) {
        String[] ret = null;
        try {
            if (hostName.length() > 0) {
                InetAddress[] addrs = InetAddress.getAllByName(hostName);
                if (addrs.length > 0) {
                    ret = new String[addrs.length];
                    for (int i = 0; i < addrs.length; i++) {
                        ret[i] = addrs[i].getHostAddress();
                    }
                }
            }

        } catch (Exception ex) {
            ret = null;
        }
        return ret;
    }

    public static List<String> getLinuxHostIP() {
        List<String> ret = new ArrayList<String>();
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                System.out.println(netInterface.getName());
                Enumeration addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        ret.add(ip.getHostAddress());
                        System.out.println("本机的IP = " + ip.getHostAddress() + " .HostName = " + ip.getCanonicalHostName());
                    }
                }
            }
        } catch (SocketException ex) {
            Logger.getLogger(NetTools.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ret;
    }

    public static String getLinuxEth0IP() {
        String ret = "";
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                System.out.println(netInterface.getName());
                if (netInterface.getName().equals("eth0")) {
                    Enumeration addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = (InetAddress) addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            ret = ip.getHostAddress();
                            System.out.println("本机的IP = " + ip.getHostAddress() + " .HostName = " + ip.getCanonicalHostName());
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            Logger.getLogger(NetTools.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ret;
    }

    public static String getIP() {
        boolean linux = true;
        if (linux) {
            return getLocalHostIP();
//            return getLinuxEth0IP();
        } else {
            return getLocalHostIP();
        }
    }

    public static void main(String[] args) {
        System.out.println(getLocalHostIP());
        System.out.println("主机名：" + getLocalHostName());

        String[] localIP = getAllLocalHostIP();
        for (int i = 0; i < localIP.length; i++) {
            System.out.println(localIP[i]);
        }

        getLinuxHostIP();

        getLinuxEth0IP();

        getIP();
    }
}
